//
//  YAYISendResumeListViewController.m
//  YAYIDoctor
//
//  Created by Veer on 16/7/1.
//
//

#import "YAYISendResumeListViewController.h"
#import "YAYISendResumeDetailViewController.h"

#import "YAYISendResumeListCell.h"
#import "YAYISendResumeListModel.h"

@interface YAYISendResumeListViewController ()

// 没有数据显示提示
@property (nonatomic, weak) UILabel *noDataFlagLbl;

@property (nonatomic, strong) NSMutableArray *sendedResumeArray;

// 分页相关
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation YAYISendResumeListViewController


- (NSMutableArray *)sendedResumeArray
{
    if (nil == _sendedResumeArray) {
        _sendedResumeArray = [NSMutableArray array];
    }
    return _sendedResumeArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Uncomment the following line to preserve selection between presentations.
    self.title = @"我的预约";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
    
    __block typeof(self) weakself = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakself getSendResumelList];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself getMoreSendResumelList];
    }];
    
//    [self setupNav];
//    [self getSendResumelList];
    [self.tableView.mj_header beginRefreshing];
}


- (void)setupNav
{
    NSString *rightBtnTitleStr = @"添加";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [button setImage:[UIImage imageNamed:@"back_button"] forState:UIControlStateNormal];
    [button setTitle:rightBtnTitleStr forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    
    button.frame = (CGRect){{0, 0}, CGSizeMake(70, 30)};
    
    // 让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [button addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改导航栏左边的item
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}


/**
 *  没有数据时显示
 */
- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"暂时没有数据";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView);
        make.centerY.equalTo(self.tableView).offset(-90);
    }];
}


#pragma mark - 获取数据
- (void)getSendResumelList
{
    [self.sendedResumeArray removeAllObjects];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentPage"] = @(1);
    
    [YAYINetWorking getWithURL:selectSendResume_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            
            YAYILog(@"返回：%@", dict);
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                return;
            }
            
            NSArray *dictArray = dict[@"data"];
            for (NSDictionary *dict in dictArray) {
                YAYISendResumeListModel *model = [[YAYISendResumeListModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.sendedResumeArray addObject:model];
            }
            
            if (dictArray.count < 10) {
                
                self.tableView.mj_footer.hidden = YES;
            }
            
            self.currentPage = 1;
            [self.tableView.mj_header endRefreshing];
            if (self.sendedResumeArray.count < 10) {
                
                [self.tableView.mj_footer endRefreshing];
                self.tableView.mj_footer.hidden = YES;
            }else{
                [self.tableView.mj_footer setState:MJRefreshStateIdle];
                self.tableView.mj_footer.hidden = NO;
            }
            
            if (0 == self.sendedResumeArray.count) {
                [self showNoDataFlag:YES];
                
            }else{
                [self showNoDataFlag:NO];
            }
            
            [self.tableView reloadData];
        }else{
            
        }
        // 取消显示hud
        // [SVProgressHUD dismiss];
    }];
}


- (void)getMoreSendResumelList
{
    
    self.currentPage++;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentPage"] = @(self.currentPage + 1);
    
    [YAYINetWorking getWithURL:selectSendResume_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            
            if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                return;
            }
            
            NSArray *dictArray = dict[@"data"];
            for (NSDictionary *dict in dictArray) {
                YAYISendResumeListModel *model = [[YAYISendResumeListModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.sendedResumeArray addObject:model];
            }
            
            // 当前页数+1
            if (dictArray.count < 10) {
                [self.tableView.mj_footer endRefreshing];
                self.tableView.mj_footer.hidden = YES;
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            
        }else{
            
        }
        // 取消显示hud
        // [SVProgressHUD dismiss];
    }];
}


#pragma mark - 点击事件



#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.sendedResumeArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YAYISendResumeListModel *model = self.sendedResumeArray[indexPath.row];
    
    YAYISendResumeListCell *cell = [YAYISendResumeListCell cellWith:tableView];
    cell.model = model;
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YAYISendResumeListModel *model = self.sendedResumeArray[indexPath.row];
    
    YAYISendResumeDetailViewController *sendResumeDetailVc = [[YAYISendResumeDetailViewController alloc] init];
    sendResumeDetailVc.jobid = [NSString stringWithFormat:@"%ld", model.jobid];
    [self.navigationController pushViewController:sendResumeDetailVc animated:YES];
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        YAYILog(@"点击了删除");
        
        YAYISendResumeListModel *model = self.sendedResumeArray[indexPath.row];
        // 删除标签
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"sendid"] = @(model.sendid);
        
        YAYILog(@"param:%@", param);
        
        [YAYINetWorking postJsonSerilizeWithURL:deleteResumeSend_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error,NSDictionary *responseError) {
            if (error == nil) {
                YAYILog(@"删除已投递简历：%@", dict);
                
                if (dict == nil || [dict isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                    [SVProgressHUD showWithStatus:@"删除失败"];
                    
                }else{
                    
                    [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                    [self.sendedResumeArray removeObjectAtIndex:indexPath.row];
                    
                    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                }
            }else{
                
                NSString *message = responseError[@"message"];
                [SVProgressHUD showErrorWithStatus:message];
            }
        }];
    }
}




@end
